package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.pojo.Setmeal;
import com.itheima.mapper.SetmealMapper;
import com.itheima.service.ISetmealService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-12-19
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements ISetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Setmeal> page = setmealMapper.findPage(queryPageBean);
        return new PageResult(page.getTotal(),page.getResult());

    }

    @Override
    public Setmeal findById(Integer id) {
        return setmealMapper.findById(id);
    }

    @Override
    public void add(Setmeal setmeal, List<Integer> checkGroupIds) {
        try {
            setmealMapper.add(setmeal);
            Integer id = setmeal.getId();
            if (checkGroupIds != null && checkGroupIds.size() > 0) {
                setmealMapper.setSetmealAndCheckGroup(id, checkGroupIds);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void edit(Setmeal setmeal, List<Integer> checkGroupIds) {
        try {
            String imgFilePath =setmealMapper.getImgFilePath(setmeal.getId());
            redisTemplate.delete(imgFilePath);
            setmealMapper.edit(setmeal);
            Integer id = setmeal.getId();
            setmealMapper.deleteSetmealAndCheckGroup(id);
            if (checkGroupIds != null && checkGroupIds.size() > 0) {
                setmealMapper.setSetmealAndCheckGroup(id, checkGroupIds);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void deleteById(Integer id) {
        setmealMapper.deleteByGroupId(id);
        setmealMapper.deleteBySetmealId(id);
    }

    /**
     * 生成套餐详情的静态页面
     * @param fileName 文件名
     * @param id 套餐ID
     */
    /**
     * 生成套餐列表的静态页面
     * @param fileName 文件名
     */
    @Override
    public void generateFile(String fileName) {
        // 获取配置对象
        Configuration configuration = freeMarkerConfigurer.getConfiguration();
        try {
            // 获取模板对象
            Template template = configuration.getTemplate(fileName + ".ftl");
            // 获取数据
            List<Setmeal> setmealList = setmealMapper.selectAll();
            if (setmealList == null || setmealList.isEmpty()) {
                System.out.println("当前套餐为空");
                return;
            }
            // 创建数据模型
            Map<String, Object> dataModel = new HashMap<>();
            dataModel.put("setmealList", setmealList);
            // 生成静态页面
            File file = new File("D:\\daima1\\bxg-health-parent\\bxg-health-backend\\src\\main\\resources\\templates" + "setmeal.html");
            try (
                    // 创建输出流
                    Writer writer = new FileWriter(file)
            ) {
                // 处理模板并生成文件
                template.process(dataModel, writer);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
